﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;

namespace G.Controls
{
    public class Window : ContentControl
    {
        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(Window), new PropertyMetadata(false, new PropertyChangedCallback(Window.OnIsOpenChanged)));

        public bool IsOpen
        {
            get
            {
                return (bool)base.GetValue(IsOpenProperty);
            }
            set
            {
                base.SetValue(IsOpenProperty, value);
            }
        }

        private static void OnIsOpenChanged(object sender, DependencyPropertyChangedEventArgs A_1)
        {
            Window window = sender as Window;
            if (window != null) window.OnIsOpenChanged();
        }

        public static readonly DependencyProperty HorizontalOffsetPorperty = DependencyProperty.Register("HorizontalOffset", typeof(double), typeof(Window), new PropertyMetadata(0.0));

        public double HorizontalOffset
        {
            get
            {
                return (double)this.GetValue(HorizontalOffsetPorperty);
            }
            set
            {
                this.SetValue(HorizontalOffsetPorperty, value);
            }
        }


        public static readonly DependencyProperty VerticalOffsetPorperty = DependencyProperty.Register("VerticalOffset", typeof(double), typeof(Window), new PropertyMetadata(0.0));

        public double VerticalOffset
        {
            get
            {
                return (double)this.GetValue(VerticalOffsetPorperty);
            }
            set
            {
                this.SetValue(VerticalOffsetPorperty, value);
            }
        }


        private Popup popup;
        private Canvas window;

        internal PhoneApplicationFrame Frame
        {
            get
            {
                return Application.Current.RootVisual as PhoneApplicationFrame;
            }
        }
        private PhoneApplicationPage page;
        internal PhoneApplicationPage Page
        {
            get
            {
                if (page == null)
                {
                    page = Frame.Content as PhoneApplicationPage;
                }
                return page;
            }
        }

        void rootVisual_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.OnBackKeyPress(e);
        }

        public Window()
        {
            this.DefaultStyleKey = typeof(Window);
            popup = new Popup();
            window = new Canvas();
            popup.Child = window;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        private void OnIsOpenChanged()
        {
            if (this.IsOpen)
            {
                OnOpening();
                if (window.Children.Count == 0)
                {
                    this.SetBinding(Canvas.TopProperty, BindingHelp.Create("VerticalOffset", this));
                    this.SetBinding(Canvas.LeftProperty, BindingHelp.Create("HorizontalOffset", this));
                    window.Children.Add(this);
                }
                popup.IsOpen = true;
                this.Frame.BackKeyPress += rootVisual_BackKeyPress;
                OnOpened();
            }
            else
            {
                popup.IsOpen = false;
                this.Frame.BackKeyPress -= rootVisual_BackKeyPress;
                OnClosed();
            }
        }

        protected virtual void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

        }

        protected virtual void OnOpening()
        {

        }

        protected virtual void OnOpened()
        {

        }

        protected virtual void OnClosed()
        {

        }
        void page_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.IsOpen = false;
        }

    }
}
